let util = {
    // 封装接送模块
    tojson: function (res, result) {
        if (typeof result.result === 'undefined') {
            res.json({
                code: '400',
                message: '后台异常,操作失败'
            });
        } else if (result.result === 'add') {
            res.json({
                code: '200',
                message: '添加成功'
            });
        } else if (result.result === 'delete') {
            res.json({
                code: '200',
                message: '删除成功'
            });
        } else if (result.result === 'update') {
            res.json({
                code: '200',
                message: '更改成功'
            });
        } else if (result.result === 'replace') {
            res.json({
                code: '200',
                message: '替换成功'
            });
        } else if (result.result != 'undefined' && result.result === 'select') {
            res.json({
                code: '200',
                message: '查找成功',
                //total: result.data.length,
                data: result.data
            });
        } else if (result.result != 'undefined' && result.result === 'selectall') {
            res.json({
                code: '200',
                message: '全部查找成功',
                //total: result.data.length,
                data: result.data
            });
        } else if (result.result != 'undefined' && result.result === 'page') {
            res.json({
                code: '200',
                message: '查找成功',
                total: result.data[0][0]["total"],
                data: result.data[1]
            });
        } else {
            res.json(result);
        }
    },
    // 判断是否为数组
    isArray: function (o) {
        return Object.prototype.toString.call(o) == '[object Array]';
    },
    // 获取列名和值数组
    getColValue: function (jsondata) {
        let cols = "";
        let values = "";
        for (var item in jsondata) {
            cols += "," + item;
            if (typeof (jsondata[item]) == 'string')
                values += ",'" + jsondata[item] + "'";
            else
                values += "," + jsondata[item] + "";
        }
        return {
            cols: cols.substr(1),
            values: values.substr(1)
        }
    },
    // 获取update sql语句
    getUpdate: function (jsondata) {
        let updatesql = "";
        for (var item in jsondata) {
            if (typeof (jsondata[item]) == 'string')
                updatesql += "," + item + "='" + jsondata[item] + "'";
            else
                updatesql += "," + item + "= " + jsondata[item] + "";
        }
        return updatesql.substr(1);

    },
    // 时间format
    dateFormat: function (date) {
        let newtime = date.getFullYear() + "-" + this.PrefixZero((date.getMonth() + 1), 2) + "-" + this.PrefixZero(date.getDate(), 2) +
            " " + this.PrefixZero(date.getHours(), 2) + ":" + this.PrefixZero(date.getMinutes(), 2) +
            ":" + this.PrefixZero(date.getSeconds(), 2); //+ "." + date.getMilliseconds();
        return newtime;
    },
    // 补0
    PrefixZero: function (num, n) {
        return (Array(n).join(0) + num).slice(-n);
    },
    // 延迟等待
    sleep: function (ms) {
        return new Promise(resolve => setTimeout(resolve, ms))
    }
}

module.exports = util;